package icu.study.common.config;


import com.auth0.jwt.exceptions.TokenExpiredException;
import icu.study.common.entity.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value = TokenExpiredException.class)
    public Result TokenExpiredExceptionHandler(TokenExpiredException e) {
        e.printStackTrace();
        return new Result().exception(e.getLocalizedMessage());
    }

    @ResponseBody
    @ExceptionHandler(value = RuntimeException.class)
    public Result runtimeHandler(RuntimeException e) {
        e.printStackTrace();
        return new Result().error(e.getLocalizedMessage());
    }

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Result errorHandler(Exception e) {
        e.printStackTrace();
        return new Result().exception(e.getLocalizedMessage());
    }
}
